home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 112_01.zip / NRO.C < prev    next >
Text File  |  1993-06-19  |  5KB  |  264 lines

  1. /*
  2.  *    Word Processor
  3.  *    similar to Unix NROFF or RSX-11M RNO -
  4.  *    adaptation of text processor given in
  5.  *    "Software Tools", Kernighan and Plauger.
  6.  *
  7.  *    Stephen L. Browning
  8.  *    5723 North Parker Avenue
  9.  *    Indianapolis, Indiana 46220
  10.  */
  11.  
  12. #include "a:bdscio.h"
  13. #include "nro.h"
  14. #include "nrocom.c"
  15.  
  16. main(argc,argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     int ifp, ofp;
  21.     int i;
  22.     int swflg;
  23.  
  24.     swflg = FALSE;
  25.     pout = stdout;
  26.     ifp = ofp = 0;
  27.     init();
  28.     for (i=1; i<argc; ++i) {
  29.         if (*argv[i] == '-' || *argv[i] == '+') {
  30.             if (pswitch(argv[i],&swflg) == ERR) exit(-1);
  31.         }
  32.         else if (*argv[i] == '>') {
  33.             if (ofp == 0) {
  34.                 if (!strcmp(argv[i]+1,"$P")) {
  35.                     ofp = 1;
  36.                     co.lpr = TRUE;
  37.                 }
  38.                 else if ((ofp = fcreat(argv[i]+1,&oub)) == ERR) {
  39.                     printf("nro: cannot create %s\n",argv[i]+1);
  40.                     exit(-1);
  41.                 }
  42.                 else {
  43.                     pout = &oub;
  44.                 }
  45.             }
  46.             else {
  47.                 puts("nro: too many output files\n");
  48.                 exit(-1);
  49.             }
  50.         }
  51.     }
  52.     for (i=1; i<argc; ++i) {
  53.         if (*argv[i] != '-' && *argv[i] != '+' && *argv[i] != '>') {
  54.             if ((ifp = fopen(argv[i],&sofile[0])) == ERR) {
  55.                 printf("nro: unable to open file %s\n",argv[i]);
  56.                 exit(-1);
  57.             }
  58.             else {
  59.                 profile();
  60.                 fclose(&sofile[0]);
  61.             }
  62.         }
  63.     }
  64.     if ((ifp == 0 && swflg == FALSE) || argc <= 1) {
  65.         puts("Usage: nro [-n] [+n] [-pxx] [-v] [-b] [-mmacfile] infile ... [>outfile]\n");
  66.         exit(-1);
  67.     }
  68.     if (pout != stdout) {
  69.         putc(CPMEOF,pout);
  70.         fflush(pout);
  71.         fclose(pout);
  72.     }
  73. }
  74.  
  75.  
  76.  
  77. /*
  78.  *    retrieve one line of input text
  79.  */
  80.  
  81. getlin(p,in_buf)
  82. char *p;
  83. struct _buf *in_buf;
  84. {
  85.     int i;
  86.     int c;
  87.     char *q;
  88.  
  89.     q = p;
  90.     for (i=0; i<MAXLINE-1; ++i) {
  91.         c = ngetc(in_buf);
  92.         if (c == CPMEOF || c == EOF) {
  93.             *q = EOS;
  94.             c = strlen(p);
  95.             return(c == 0 ? EOF : c);
  96.         }
  97.         *q++ = c;
  98.         if (c == '\n') break;
  99.     }
  100.     *q = EOS;
  101.     return(strlen(p));
  102. }
  103.  
  104.  
  105.  
  106. /*
  107.  *    initialize parameters for nro word processor
  108.  */
  109.  
  110. init()
  111. {
  112.     int i;
  113.  
  114.     dc.fill = YES;
  115.     dc.lsval = 1;
  116.     dc.inval = 0;
  117.     dc.rmval = PAGEWIDTH - 1;
  118.     dc.tival = 0;
  119.     dc.ceval = 0;
  120.     dc.ulval = 0;
  121.     dc.cuval = 0;
  122.     dc.juval = YES;
  123.     dc.boval = 0;
  124.     dc.bsflg = FALSE;
  125.     dc.pgchr = '#';
  126.     dc.cmdchr = '.';
  127.     dc.prflg = TRUE;
  128.     dc.sprdir = 0;
  129.     for (i=0; i<26; ++i) dc.nr[i] = 0;
  130.     pg.curpag = 0;
  131.     pg.newpag = 1;
  132.     pg.lineno = 0;
  133.     pg.plval = PAGELEN;
  134.     pg.m1val = 2;
  135.     pg.m2val = 2;
  136.     pg.m3val = 2;
  137.     pg.m4val = 2;
  138.     pg.bottom = pg.plval - pg.m4val - pg.m3val;
  139.     pg.offset = 0;
  140.     pg.frstpg = 0;
  141.     pg.lastpg = 30000;
  142.     pg.ehead[0] = pg.ohead[0] = '\n';
  143.     pg.efoot[0] = pg.ofoot[0] = '\n';
  144.     for (i=1; i<MAXLINE; ++i) {
  145.         pg.ehead[i] = pg.ohead[i] = EOS;
  146.         pg.efoot[i] = pg.ofoot[i] = EOS;
  147.     }
  148.     pg.ehlim[LEFT] = pg.ohlim[LEFT] = dc.inval;
  149.     pg.eflim[LEFT] = pg.oflim[LEFT] = dc.inval;
  150.     pg.ehlim[RIGHT] = pg.ohlim[RIGHT] = dc.rmval;
  151.     pg.eflim[RIGHT] = pg.oflim[RIGHT] = dc.rmval;
  152.     co.outp = 0;
  153.     co.outw = 0;
  154.     co.outwds = 0;
  155.     co.lpr = FALSE;
  156.     for (i=0; i<MAXLINE; ++i) co.outbuf[i] = EOS;
  157.     for (i=0; i<MXMDEF; ++i) mac.mnames[i] = NULL;
  158.     mac.lastp = 0;
  159.     mac.emb = &mac.mb[0];
  160.     mac.ppb = NULL;
  161. }
  162.  
  163.  
  164. /*
  165.  *    get character from input file or push back buffer
  166.  */
  167.  
  168. ngetc(infp)
  169. struct _buf *infp;
  170. {
  171.     int c;
  172.  
  173.     if (mac.ppb >= &mac.pbb[0]) {
  174.         c = *mac.ppb--;
  175.     }
  176.     else {
  177.         c = getc(infp);
  178.     }
  179.     return(c);
  180. }
  181.  
  182.  
  183.  
  184. /*
  185.  *    process input files from command line
  186.  */
  187.  
  188. profile()
  189. {
  190.     char ibuf[MAXLINE];
  191.  
  192.     for (dc.flevel=0; dc.flevel>=0; --dc.flevel) {
  193.         while (getlin(ibuf,&sofile[dc.flevel]) != EOF) {
  194.             if (ibuf[0] == dc.cmdchr) comand(ibuf);
  195.             else text(ibuf);
  196.         }
  197.         if (dc.flevel > 0) fclose(&sofile[dc.flevel]);
  198.     }
  199.     if (pg.lineno > 0) space(HUGE);
  200. }
  201.  
  202.  
  203.  
  204. /*
  205.  *    process switch values from command line
  206.  */
  207.  
  208. pswitch(p,q)
  209. char *p;
  210. int *q;
  211. {
  212.     int swgood;
  213.  
  214.     swgood = TRUE;
  215.     if (*p == '-') {
  216.         switch (tolower(*++p)) {
  217.         case 'b':
  218.             dc.bsflg = TRUE;
  219.             break;
  220.         case 'm':
  221.             if (fopen(++p,&sofile[0]) == ERR) {
  222.                 printf("***nro: unable to open file %s\n",p);
  223.                 exit(-1);
  224.             }
  225.             profile();
  226.             fclose(&sofile[0]);
  227.             break;
  228.         case 'p':
  229.             set(&pg.offset,ctod(++p),'1',0,0,HUGE);
  230.             break;
  231.         case 'v':
  232.             printf("NRO version 1.0\n");
  233.             *q = TRUE;
  234.             break;
  235.         case '0':
  236.         case '1':
  237.         case '2':
  238.         case '3':
  239.         case '4':
  240.         case '5':
  241.         case '6':
  242.         case '7':
  243.         case '8':
  244.         case '9':
  245.             pg.lastpg = ctod(p);
  246.             break;
  247.         default:
  248.             swgood = FALSE;
  249.             break;
  250.         }
  251.     }
  252.     else if (*p == '+') {
  253.         pg.frstpg = ctod(++p);
  254.     }
  255.     else {
  256.         swgood = FALSE;
  257.     }
  258.     if (swgood == FALSE) {
  259.         printf("nro: illegal switch %s\n",p);
  260.         return(ERR);
  261.     }
  262.     return(OK);
  263. }
  264.